package com.hishop.product.controller;

import com.hishop.common.utils.PageUtils;
import com.hishop.common.utils.R;
import com.hishop.product.dao.AttrDao;
import com.hishop.product.dao.AttrGroupDao;
import com.hishop.product.entity.AttrEntity;
import com.hishop.product.entity.AttrGroupEntity;
import com.hishop.product.service.AttrAttrgroupRelationService;
import com.hishop.product.service.AttrGroupService;
import com.hishop.product.service.AttrService;
import com.hishop.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author tanwenbin
 * @email 2466082499@qq.com
 * @date 2024-03-13 09:56:53
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrDao attrDao;

    /**
     * 规则参数列表
     * type 为 base 开头的路径的时候查询基础属性
     * 为 sale 开头的路径的时候查询基础属性
     */
    @RequestMapping("/base/list/{catId}")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catId") Long catId) {
        PageUtils page = attrService.queryPage(params, catId);
        return R.ok().put("page", page);
    }

    /**
     * 销售属性列表
     * @param params
     * @param catId
     * @return
     */
    @RequestMapping("/sale/list/{catId}")
    //@RequiresPermissions("product:attr:list")
    public R saleList(@RequestParam Map<String, Object> params,
                      @PathVariable("catId") Long catId) {
        Long type = 0L;
        PageUtils page = attrService.queryPage(params, catId,type);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {

        AttrEntity attr = attrService.getById(attrId);// 获取属性 对象

        attrAttrgroupRelationService.selectGroupName(attr, attrId);
        // 分类service配置一个节点路径的查找
        Long[] path = categoryService.findCatelogPath(attr.getCatelogId());
        attr.setCatelogPath(path);

        List<AttrGroupEntity> attrGroups = attrGroupDao.selectList();
        for (AttrGroupEntity a : attrGroups) {
            a.setCatelogPath(categoryService.findCatelogPath(a.getCatelogId()));
        }
        attr.setAttrGroups(attrGroups);
        System.out.println("@@@@@@@@" + attrGroups.toString());
        // 在这返回，或者是设置在 attr属性对象里面
//         return R.ok().put("attr", attr).put("attrGroups",attrGroups);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrEntity attr) {
        // 设置分组列表
        attr.setAttrGroups(attrGroupDao.selectList());
        // 获取 分组id、根据分组id获取分组实体
        Long attrGroupId = attr.getAttrGroupId();
        AttrGroupEntity attrGroup = attrGroupDao.selectById(attrGroupId);
        // 设置分组名称、分组id
        attr.setGroupName(attrGroup.getAttrGroupName());
        attr.setAttrGroupId(attrGroupId);

        attrService.save(attr);// 保存之后才有 attrId

        attrAttrgroupRelationService.saveDetail(attr);// 新增关联表

//        System.out.println("save111111111:" + attr.toString());
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrEntity attr) {
        attrService.updateDetail(attr);


        System.out.println("attr:---------------" + attr.toString());
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }
}
